package com.yunze.web.controller.yunze.fhgl;

import com.alibaba.fastjson.JSON;
import com.yunze.common.annotation.Log;
import com.yunze.common.core.controller.BaseController;
import com.yunze.common.core.domain.entity.SysUser;
import com.yunze.common.core.domain.model.LoginUser;
import com.yunze.common.enums.BusinessType;
import com.yunze.common.utils.ServletUtils;
import com.yunze.common.utils.ip.IpUtils;
import com.yunze.common.utils.spring.SpringUtils;
import com.yunze.common.utils.yunze.AesEncryptUtil;
import com.yunze.common.utils.yunze.Different;
import com.yunze.framework.web.service.TokenService;
import com.yunze.system.service.yunze.fhgl.IFhglShippingApplicationService;
import com.yunze.system.service.yunze.xsgl.customer.IYzXsglCustomerService;
import com.yunze.web.core.config.MyBaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发货管理 发货申请
 */
@RestController
@RequestMapping("/yunze/ShippingApplication")
public class FhglShippingApplicationController extends MyBaseController
{

    @Resource
    private IFhglShippingApplicationService iFhglShippingApplicationService;
    @Resource
    private IYzXsglCustomerService iYzXsglCustomerService;


    /**
     * 发货申请 列表
     */
    @PreAuthorize("@ss.hasPermi('yunze:ShippingApplication:list')")
    @PostMapping(value = "/list", produces = { "application/json;charset=UTF-8" })
    public String list(@RequestBody String Pstr)
    {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if(Pstr!=null){
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr =  AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject((String) Pstr));
            try {
                //获取对当前登录用户 菜单根目录 允许 btn 权限
                LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
                SysUser User = loginUser.getUser();
                HashMap<String, Object> AuthorityMap = new HashMap<String, Object>();
                AuthorityMap.put("user_id",User.getUserId());
                List<Map<String,Object>> UserArr = iYzXsglCustomerService.findSalesPartner(AuthorityMap);
                Object Btns = (UserArr.get(0)).get("Btns");
                if(Btns!=null && Btns.toString().length()>0){
                    String arr[] = Btns.toString().split(",");
                    if(!Different.Is_existence(arr,"yunze:sys:ShipmentApplicationSelAll")){// 默认仅可看自己数据 【允许查看所有发货申请】
                        List<String> sales_idArr = new ArrayList<>();
                        sales_idArr.add(""+User.getUserId());
                        Parammap.put("sales_idArr",sales_idArr);
                    }
                }
            }catch (Exception e){
                System.out.println("仅可看所属销售 权限过滤操作失败");
            }
            return MyRetunSuccess(iFhglShippingApplicationService.selMap(Parammap),null);
        }catch (Exception e){
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/>  ShippingApplication/list  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ",e.getCause().toString());
        }
        return Myerr("获取发货申请 列表 操作失败！");
    }



    /**
     * 发货申请 新增
     */
    @Log(title = "发货申请", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('yunze:ShippingApplication:add')")
    @PostMapping(value = "/add", produces = { "application/json;charset=UTF-8" })
    public String add(@RequestBody String Pstr)
    {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if(Pstr!=null){
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr =  AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject((String) Pstr));


            LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
            SysUser User = loginUser.getUser();
            Parammap.put("create_UserId",User.getDeptId());
            boolean bool = iFhglShippingApplicationService.add_SAN(Parammap);
            if(bool){
                return MyRetunSuccess("操作成功！",null);
            }else{
                return Myerr("网络操作失败请稍后重试！");
            }
        }catch (Exception e){
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/>  ShippingApplication/list  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ",e.getCause().toString());
        }
        return Myerr("新增 发货申请 操作失败！");
    }


    /**
     * 查询商品已发货数量
     */
    @PreAuthorize("@ss.hasPermi('yunze:ShippingApplication:add')")
    @PostMapping(value = "/sel_SUM_Sent_Quantity", produces = { "application/json;charset=UTF-8" })
    public String sel_SUM_Sent_Quantity(@RequestBody String Pstr)
    {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if(Pstr!=null){
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr =  AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject((String) Pstr));
            return MyRetunSuccess(iFhglShippingApplicationService.sel_SUM_Sent_Quantity(Parammap),"操作成功！");
        }catch (Exception e){
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/>  ShippingApplication/list  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ",e.getCause().toString());
        }
        return Myerr("查询商品已发货数量 操作失败！");
    }




    /**
     * 查询 发货申请详情
     */
    @PreAuthorize("@ss.hasPermi('yunze:ShippingApplication:find')")
    @PostMapping(value = "/findData", produces = { "application/json;charset=UTF-8" })
    public String findData(@RequestBody String Pstr)
    {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if(Pstr!=null){
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr =  AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject((String) Pstr));
            return MyRetunSuccess(iFhglShippingApplicationService.find_data(Parammap),"操作成功！");

        }catch (Exception e){
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/>  ShippingApplication/findData  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ",e.getCause().toString());
        }
        return Myerr("查询 发货申请详情 操作失败！");
    }




    /**
     * 修改 发货申请
     */
    @Log(title = "发货申请", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('yunze:ShippingApplication:upd')")
    @PostMapping(value = "/Updinfo", produces = { "application/json;charset=UTF-8" })
    public String Updinfo(@RequestBody String Pstr)
    {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if(Pstr!=null){
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr =  AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject((String) Pstr));
            boolean bool = iFhglShippingApplicationService.upd_SAN(Parammap);
            if(bool){
                return MyRetunSuccess("操作成功！",null);
            }else{
                return Myerr("网络操作失败请稍后重试！");
            }
        }catch (Exception e){
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/>  ShippingApplication/Updinfo  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ",e.getCause().toString());
        }
        return Myerr("修改 发货申请 操作失败！");
    }


    /**
     * 删除 发货申请
     */
    @Log(title = "发货申请", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('yunze:ShippingApplication:del')")
    @PostMapping(value = "/DelInfo", produces = { "application/json;charset=UTF-8" })
    public String DelInfo(@RequestBody String Pstr)
    {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if(Pstr!=null){
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr =  AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject((String) Pstr));
            boolean bool = iFhglShippingApplicationService.del_SAN(Parammap);
            if(bool){
                return MyRetunSuccess("操作成功！",null);
            }else{
                return Myerr("网络操作失败请稍后重试！");
            }
        }catch (Exception e){
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/>  ShippingApplication/Updinfo  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ",e.getCause().toString());
        }
        return Myerr("删除 发货申请 操作失败！");
    }


    /**
     *查询 单号
     */
    @PreAuthorize("@ss.hasPermi('yunze:ShippingApplication:oddNumbers')")
    @PostMapping(value = "/oddNumbers", produces = {"application/json;charset=UTF-8"})
    public String Odd_Numbers(@RequestBody String Pstr) {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if (Pstr != null) {
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr = AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject(Pstr));
            return MyRetunSuccess(iFhglShippingApplicationService.sleNumber(Parammap), null);
        } catch (Exception e) {
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/> yunze:ShippingApplication:del  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ", e.getCause().toString());
        }
        return Myerr("查询单号 操作失败！");
    }



    /**
     *  查询 出库 合同发货 && 普通发货
     */
    @PreAuthorize("@ss.hasPermi('yunze:ShippingApplication:findOut')")
    @PostMapping(value = "/findOut", produces = { "application/json;charset=UTF-8" })
    public String findOut(@RequestBody String Pstr)
    {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if(Pstr!=null){
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr =  AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject((String) Pstr));
            return MyRetunSuccess(iFhglShippingApplicationService.find_OutData(Parammap),"操作成功！");

        }catch (Exception e){
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/>  ShippingApplication/findOut  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ",e.getCause().toString());
        }
        return Myerr("查询 单条需要出库的发货申请 操作失败！");
    }




}












